<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :span="24">
            <a-form-item label="产品名称">
              <a-input placeholder="请输入产品名称" v-model="queryParam.productName"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="产品类型">
              <a-tree-select
                allowClear
                v-model="queryParam.productType"
                style="width: 100%"
                :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
                :tree-data="treeData"
                :replaceFields="dataFields"
                placeholder="请选择类型"
                @change="changeParentId"
              >
              </a-tree-select>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-button type="primary" @click="searchQuery" icon="search" style="width: 100%">查询</a-button>
          </a-col>
          <a-col :span="12">
            <a-button type="primary" @click="searchReset" icon="reload" style="width: 100%">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus" style="width: 100%;margin-top: 10px">新增</a-button>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <!--<div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>-->

      <div class="list">
        <van-list
          v-model="loading"
          :finished="finished"
          finished-text="没有更多了"
          @load="onLoadList"
        >
          <div
            class="customer-list"
            v-for="(item,i) in dataSource"
            :key="i">
            <div>
              <div class="name">{{item.productName || ''}}</div>
              <div class="contact">
                <span>产品售价：{{ item.productPrice || '无' }}</span>
                <span style="float: right">
                  <a @click="handleEdit(item)">编辑</a>
                  <a-divider type="vertical" />
                  <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(item.id)">
                    <a>删除</a>
                  </a-popconfirm>
                </span>
              </div>
              <div class="contact">
                <span>单位：{{ item.unit || '无'}}</span>
              </div>
            </div>
          </div>
        </van-list>
      </div>

    </div>

    <add-product-modal ref="modalForm" @ok="modalFormOk"></add-product-modal>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import addProductModal from './modules/addProductModal'
  import {getAction} from '@/api/manage'
  import {deleteAction} from "../../../../api/manage";

  export default {
    name: 'productList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      addProductModal,
    },
    data () {
      return {
        description: '产品管理表管理页面',
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'产品名称',
            align:"center",
            dataIndex: 'productName'
          },
          {
            title:'产品类型',
            align:"center",
            dataIndex: 'productTypeName',
          },
          {
            title:'产品售价',
            align:"center",
            dataIndex: 'productPrice'
          },
          {
            title:'单位',
            align:"center",
            dataIndex: 'unit'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/ghProductManage/list",
          delete: "/ghProductManage/delete",
          deleteBatch: "/ghProductManage/deleteBatch",
          exportXlsUrl: "/ghProductManage/exportXls",
          importExcelUrl: "/ghProductManage/importExcel",
          getParent: "/ghProductType/getProductParent"

        },
        treeData: [],
        dataFields: {
          title: 'productType',
          key: 'id',
          value: 'id',
          children: "children"
        },
        dictOptions:{},
        superFieldList:[],
        finished: false,
      }
    },
    created() {
      this.initData();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      //下一页
      onLoadList(){
        this.ipagination.current++
        this.loadData(this.ipagination.current)
      },
      loadData(arg){
        //加载数据 若传入参数1则加载第一页的内容
        if (arg === 1) {
          this.ipagination.current = 1;
          this.dataSource = [];
          this.finished = false;
        }
        let params = this.getQueryParams();//查询条件
        this.loading = true
        getAction(this.url.list, params).then((res) => {
          if (res.success) {
            this.dataSource = [...this.dataSource, ...res.result.records]
            if (res.result.total) {
              this.ipagination.total = res.result.total
            } else {
              this.ipagination.total = 0
            }
          } else {
            this.$message.warning(res.message)
          }
          this.loading = false
          if(res.result.records.length<10){
            this.finished = true;
          }
        }).finally(() => {
          this.loading = false
        })
      },
      initData() {
        getAction(this.url.getParent).then(res => {
          if (res.success) {
            this.treeData = res.result;
          }
        });
      },
      changeParentId(value){
        this.loadData();
      },
      handleDelete(id) {
        if(!this.url.delete){
          this.$message.error("请设置url.delete属性!")
          return
        }
        let that = this;
        that.loading = true;
        deleteAction(that.url.delete, {id: id}).then((res) => {
          if (res.success) {
            //重新计算分页问题
            that.$message.success(res.message);
            that.loadData(1);
          } else {
            that.$message.warning(res.message);
          }
        }).finally(() => {
          this.loading = false
        });
      },
    }
  }
</script>
<style scoped lang="less">
  @import '~@assets/less/common.less';
  .select-pk{
    display:flex;
    align-items:center;
    margin:10px 0;
    &>span{
      font-weight: bold;
    }
    &-text{
      width: calc(100% - 6em);
      height: 30px;
      text-indent: 1em;
      line-height: 28px;
      border: 1px solid #d9d9d9;
      border-radius: 4px;
    }
  }
  .list{
    overflow-y: scroll;
    height:100vh;
    margin-top: 10px;
  }
  ::-webkit-scrollbar {
    display: none;
  }
  .customer-list{
    /*display: flex;*/
    align-items: center;
    border-top: 1px solid #d9d9d9;
    .name{
      font-size: 13px;
      color: #161616;
      line-height: normal;
      margin-top: 5px;
    }
    .contact{
      margin-top: 5px;
      font-size: 12px;
      color: #a39f9f;
      margin-bottom: 5px;
    }
  }
</style>